package io.scnb.design;

import io.scnb.design.redis.RedisUtils;
import io.scnb.design.redis.cluster.EGM;
import io.scnb.design.redis.cluster.IIR;

import java.util.concurrent.TimeUnit;

/**
 * 升级版，可以使用两套集群服务，同时兼容原来的单机版redis使用方法
 */
public class CacheClusterServiceImpl implements CacheService{

    private RedisUtils redisUtils = new RedisUtils();

    private EGM egm = new EGM();

    private IIR iir = new IIR();

    @Override
    public String get(String key, int redisType) {
        if (1 == redisType) {
            return egm.gain(key);
        } else if (2 == redisType) {
            return iir.get(key);
        }
        return redisUtils.get(key);
    }

    @Override
    public void set(String key, String value, int redisType) {
        if (1 == redisType) {
            egm.set(key, value);
        } else if (2 == redisType) {
            iir.set(key, value);
        } else {
            redisUtils.set(key, value);
        }
    }

    @Override
    public void set(String key, String value, long timeout, TimeUnit timeUnit, int redisType) {
        if (1 == redisType) {
            egm.setEx(key, value, timeout, timeUnit);
        } else if (2 == redisType) {
            iir.setExpire(key, value, timeout, timeUnit);
        } else {
            redisUtils.set(key, value, timeout, timeUnit);
        }
    }

    @Override
    public void del(String key, int redisType) {
        if (1 == redisType) {
            egm.delete(key);
        } else if (2 == redisType) {
            iir.del(key);
        } else {
            redisUtils.del(key);
        }
    }
}
